package im.elephas.androfan.common.widget;

import im.elephas.androfan.R;
import im.elephas.androfan.activity.AndroFanActivity;
import im.elephas.androfan.activity.SendMessageActivity;
import im.elephas.androfan.fanfouapi.items.StatusItem;
import im.elephas.androfan.util.DateFormater;
import im.elephas.androfan.util.TagFilter;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

public class SimpleStatusWidget extends StatusWidget {

	public SimpleStatusWidget(ViewGroup parentView, AndroFanActivity mActivity) {
		super(parentView, mActivity);
		super.setWidgetView(mActivity.getLayoutInflater().inflate(
				R.layout.statusessimpleitem, null));
		initViews();
	}

	public SimpleStatusWidget(ViewGroup parentView, AndroFanActivity mActivity,
			StatusItem si) {
		this(parentView, mActivity);
		setStatusItem(si);
	}

	public void setStatusItem(StatusItem si) {
		this.statusItem = si;
		statusTextView.setText(si.getText());
		sourceTextView.setText("通过: " + TagFilter.getTag(si.getSource()));
		timeTextView.setText(DateFormater.formatDate(si.getCreatedAt()));
	}

	protected void initViews() {
		statusTextView = (TextView) super.getWidgetView().findViewById(
				R.id.statusTextView);
		sourceTextView = (TextView) super.getWidgetView().findViewById(
				R.id.sourceTextView);
		timeTextView = (TextView) super.getWidgetView().findViewById(
				R.id.timeTextView);

		getWidgetView().setClickable(true);
		
		getWidgetView().setOnLongClickListener(this);
		statusTextView.setOnLongClickListener(this);
	}

	protected void showDialog() {
		Builder dialog = new AlertDialog.Builder(mActivity);
		String[] sel = { "回复", "转发", "收藏" };
		dialog.setTitle("对@" + statusItem.getUser().getScreenName());
		dialog.setItems(sel, new Dialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i;
				Bundle b;
				switch (which) {
				case 0:
					i = new Intent(getActivity(), SendMessageActivity.class);
					b = new Bundle();
					b.putBoolean("isreply", true);
					b.putString("atname", statusItem.getUser().getName());
					b.putString("replyid", statusItem.getUser().getId());
					i.putExtras(b);
					getActivity().startActivityForResult(i, 0);
					break;
				case 1:
					getApiCaller().refanfouStatus(statusItem);
					break;
				case 2:
					getApiCaller().createFavoriteStatus(statusItem);
					break;
				}
			}
		});
		dialog.show();
	}
}
